package com.ywl.study.seata.service.impl;

import com.ywl.study.seata.service.AccountService;
import com.ywl.study.seata.service.OrderService;
import com.ywl.study.seata.util.DataSourceUtil;

import java.sql.SQLException;

public class OrderServiceImpl  implements OrderService {

    public static final String DB_KEY="order";

    private AccountService accountService;
    public void setAccountService(AccountService accountService) {
        this.accountService=accountService;
    }

    public void create(String userId, String commodityCode, Integer count) throws SQLException {
       int money=count*200;
        String sql = "insert into order_tbl (user_id, commodity_code, count, money) values ('" + userId + "','"
                + commodityCode + "'," + count + "," + money + ")";
        DataSourceUtil.executeUpdate(DB_KEY,sql);
        accountService.reduce(userId,money);
    }

    public void reset(String key, String value) throws SQLException {
        String deleteSql = "delete from order_tbl";
        DataSourceUtil.executeUpdate(DB_KEY, deleteSql);
    }
}
